
import { findHeadCategory } from '@/api/home'

// 默认的分类数据
import { category } from '@/utils/constant'
// 分类模块
export default {
  namespaced: true,
  state: {
    // 分类信息集合
    list: category.map((item, index) => ({ id: index, name: item }))
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 修改当前一级分类下的open数据为true
    show (state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = true
    },
    // 修改当前一级分类下的open数据为false
    hide (state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = false
    }
  },
  actions: {
    async getList (store, payload) {
      const res = await findHeadCategory()
      res.result.forEach(item => {
        item.open = false
      })
      store.commit('setList', res.result)
    }
  },
  getters: {

  }
}
